var date = new Date();
function closeLightBox(linkId,boxType){
	loff();
	$('#'+linkId).removeAttr('href');
	$('#'+linkId).removeClass(boxType);
	$('#loadAjaxContent').html('');
	$('.fancybox-wrap').remove();
	$('#fancybox-overlay').remove();
}
function _renderPreauthList(){
	var fund = $('#fund').val();
	var fname = $('#txt_name').val();
	var lname = $('#txt_surname').val();
	var policy_no = $('#txt_policy_no').val();
	if(require(fund))
		alert('กรุณาเลือกบริษัทประกัน');
	else if(require(policy_no) && require(fname) && require(lname))
		alert('กรุณากรอกชื่อ, นามสกุล หรือเลขกรมธรรม์เพื่อค้นหาข้อมูล');
	else if((fname || lname) && !policy_no && (require(fname) || require(lname)))
		alert('กรุณากรอกชื่อและนามสกุล');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_preauth_list',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			var fund_id = $('#fund').val();
			var searchby;
			if(policy_no)
				searchby = 'policy';
			else if(fname && lname)
				searchby = 'flname';
			else if(fname)
				searchby = 'fname';
			else if(lname)
				searchby = 'lname';
			$.ajax({
				type: "POST",
				url: (projectName+"/front/preauthorize/getpreauthbyfund/_t/"+date.getTime()).replace("//","/"),
				data: ({'fund_id':fund_id,'policy_no':policy_no,'fname':fname,'lname':lname,'searchby':searchby}),
				dataType: 'json',
				success: function(msg){
					if(msg){
						$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderPreauthConfirm(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_preauth_confirm',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var policy_id = $('#policy_id').val();
		var policy_no = $('#policy_no').val();
		var card_no = $('#card_no').val();
		var member_name = $('#member_name').val();
		var member_lname = $('#member_lname').val();
		var nat_id = $('#nat_id').val();
		var preauth_id = $('#preauth_id').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/getpreauthconfirm/_t/"+date.getTime()).replace("//","/"),
			data: ({'policy_id':policy_id,'policy_no':policy_no,'card_no':card_no,'member_name':member_name,'member_lname':member_lname,'nat_id':nat_id,'preauth_id':preauth_id}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderInsertPreauth(){
	lon();
	var preauth_id = $('#preauth_id').val();
	var user_id = $('#user_id').val();
	var policy_no = $('#policy_no').val();
	var policy_id = $('#policy_id').val();
	var preauth_datetime = $('#preauth_datetime').val();
	var customer_type = $('#customer_type_desc').val();
	$.ajax({
		type: "POST",
		url: (projectName+"/front/preauthorize/insertpreauth/_t/"+date.getTime()).replace("//","/"),
		data: ({'preauth_id':preauth_id,'user_id':user_id,'policy_no':policy_no,'policy_id':policy_id,'customer_type_desc':customer_type,'preauth_datetime':preauth_datetime}),
		dataType: 'json',
		success: function(msg){
			loff();
			alert('บันทึกข้อมูลเรียบร้อยแล้ว');
			window.open(app_url+'/print/'+policy_id,'_blank');
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		}
	});
}
function _renderPreauthListByIDCard(){
	var fund = $('#fund').val();
	var nat_id = $('#txt_idcard').val();
	if(require(fund))
		alert('กรุณาเลือกบริษัทประกัน');
	else if(require(nat_id))
		alert('กรุณากรอกหมายเลขบัตรประชาชน เพื่อค้นหาข้อมูล');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_preauth_list',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			var fund_id = $('#fund').val();
			$.ajax({
				type: "POST",
				url: (projectName+"/front/preauthorize/getpreauthbyidcard/_t/"+date.getTime()).replace("//","/"),
				data: ({'fund_id' :  fund_id,'nat_id':nat_id}),
				dataType: 'json',
				success: function(msg){
					if(msg){
							$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderPaDetail(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_pa_detail',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var policy_no = $('#policy_no').val();
		var card_no = $('#card_no').val();
		var member_name = $('#member_name').val();
		var member_lname = $('#member_lname').val();
		var nat_id = $('#nat_id').val();
		var preauth_id = $('preauth_id').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/getpreauthbypadetail/_t/"+date.getTime()).replace("//","/"),
			data: ({'policy_no':policy_no,'card_no':card_no,'member_name':member_name,'member_lname':member_lname,'nat_id':nat_id,'preauth_id':preauth_id}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}

function _renderHealthDetail(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_health_detail',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var policy_no = $('#policy_no').val();
		var card_no = $('#card_no').val();
		var member_name = $('#member_name').val();
		var member_lname = $('#member_lname').val();
		var nat_id = $('#nat_id').val();
		var preauth_id = $('preauth_id').val();
		var customer_type_desc = $('customer_type_desc').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/getpreauthbyhealthdetail/_t/"+date.getTime()).replace("//","/"),
			data: ({'policy_no':policy_no,'card_no':card_no,'member_name':member_name,'member_lname':member_lname,'nat_id':nat_id,'preauth_id':preauth_id,'customer_type_desc':customer_type_desc}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderInsertPaDetail(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/preauthorize/loadtpl/tpl/_popup_insert_pa_detail',function(){
		$('#link_preauth_fund').attr('href', '#view_preauth_list');
		$('#link_preauth_fund').addClass('fancybox-ajax-light');
		$('#link_preauth_fund').click();
		$('#closeFb').click(function(){
			closeLightBox('link_preauth_fund','fancybox-ajax-light');
		});
		var preauth_id = $('preauth_id').val();
		var user_id = $('user_id').val();
		var policy_no = $('#policy_no').val();
		var preauth_status = $('preauth_status').val();
		var customer_type_desc = $('customer_type_desc').val();
		var preauth_datetime = $('preauth_datetime').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/preauthorize/insertpreauthbypadetail/_t/"+date.getTime()).replace("//","/"),
			data: ({'preauth_id':preauth_id,'user_id':user_id,'policy_no':policy_no,'preauth_status':preauth_status,'customer_type_desc':customer_type_desc,'preauth_datetime':preauth_datetime}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					$("#tbody_view_preauth_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderPaymentList(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/payment/loadtpl/tpl/_popup_payment_list',function(){
		$('#link_payment_list').attr('href', '#view_payment_list');
		$('#link_payment_list').addClass('fancybox-ajax-light');
		$('#link_payment_list').click();
		$('#closeFb').click(function(){
			closeLightBox('link_payment_list','fancybox-ajax-light');
		});
		var userid = $('#hid_userid').val();
		var preauth_datefrom = $('#hid_preauth_datefrom').val();
		var preauth_dateto = $('#hid_preauth_dateto').val();
		var menu_num = $('input[name=rdo_payment]:checked').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/payment/getpaymentlist/_t/"+date.getTime()).replace("//","/"),
			data: ({'preauth_datefrom' : preauth_datefrom,'preauth_dateto' : preauth_dateto,'userid' :  userid,'menu_num' :  menu_num}),
			dataType: 'json',
			success: function(msg){
				if(msg){
						$("#tbody_view_payment_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}

function _renderPaymentConfirm(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/payment/loadtpl/tpl/_popup_payment_confirm',function(){
		$('#link_payment_list').attr('href', '#view_preauth_list');
		$('#link_payment_list').addClass('fancybox-ajax-light');
		$('#link_payment_list').click();
		$('#closeFb').click(function(){
			closeLightBox('link_payment_list','fancybox-ajax-light');
		});
		var payment_id	=	$('#hid_payment_id').val();
		var userid	=	$('#hid_user_id').val();
		var preauth_id	=	$('#hid_preauth_id').val();
		var policy_no	=	$('#hid_policy_no').val();
		var payment_datetime	=	$('#hid_payment_datetime').val();
		var payment_status	=	$('#hid_payment_status').val();
		var customer_type	=	$('#hid_customer_type').val();
		var policy_id		=	$('#hid_policy_id').val();
		var payment_type	=	$('input[name=rdo_type]:checked').val();
		//var payment_type	=	$('#rdo_type').val();
		var acc_date	=	$('#txt_acc_date').val();
		var acc_hour	=	$('#acc_hour').val();
		var acc_min	=	$('#acc_min').val();
		var admis_date	=	$('#txt_admis_date').val();
		var adm_hour	=	$('#adm_hour').val();
		var adm_min	=	$('#adm_min').val();
		var deductible	=	$('#txt_deductible').val();
		var charge	=	$('#txt_charge').val();
		var discount	=	$('#txt_discount').val();
		var	co_payment	=	$('#txt_co_payment').val();
		var pre_approved	=	$('#txt_pre_approved').val();
		var non_covered	=	$('#txt_non_covered').val();
		var	indiation_admit	=	$('#tar_remark').val();
		var acc_time = acc_hour+':'+acc_min+':00.000';
		var adm_time = adm_hour+':'+adm_min+':00.000';
		$.ajax({
			type: "POST",
			url: (projectName+"/front/payment/getpaymentconfirm/_t/"+date.getTime()).replace("//","/"),
			data: ({'payment_id' : payment_id,'userid' : userid,'preauth_id' :  preauth_id,'policy_no' :  policy_no,
				'payment_datetime' : payment_datetime,'payment_status' : payment_status,'customer_type' : customer_type,'policy_id' : policy_id,
				'payment_type' : payment_type,'acc_date' : acc_date,'acc_time' : acc_time,'admis_date' : admis_date,'adm_time' : adm_time,'deductible' : deductible,
				'charge' : charge,'discount' : discount,'co_payment' : co_payment,'pre_approved' : pre_approved,
				'non_covered' : non_covered,'indiation_admit' : indiation_admit}),
			dataType: 'json',
			success: function(msg){
				if(msg){
						$("#tbody_view_payment_list").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderInsertPayment(){
	lon();
	$('#loadAjaxContent').load(app_url+'/front/payment/loadtpl/tpl/_popup_insert_payment',function(){
		$('#link_payment_list').attr('href', '#view_preauth_list');
		$('#link_payment_list').addClass('fancybox-ajax-light');
		$('#link_payment_list').click();
		$('#closeFb').click(function(){
			closeLightBox('link_payment_list','fancybox-ajax-light');
		});
		var payment_id	=	$('#hid_payment_id').val();
		var userid	=	$('#hid_user_id').val();
		var preauth_id	=	$('#hid_preauth_id').val();
		var policy_no	=	$('#hid_policy_no').val();
		var payment_datetime	=	$('#hid_payment_datetime').val();
		var payment_status	=	$('#hid_payment_status').val();
		var customer_type	=	$('#hid_customer_type').val();
		var policy_id		=	$('#hid_policy_id').val();
		var payment_type	=	$('#hid_payment_type').val();
		var acc_date	=	$('#hid_acc_date').val();
		var admis_date	=	$('#hid_admis_date').val();
		var deductible	=	$('#hid_deductible').val();
		var charge	=	$('#hid_charge').val();
		var discount	=	$('#hid_discount').val();
		var	co_payment	=	$('#hid_co_payment').val();
		var pre_approved	=	$('#hid_pre_approved').val();
		var non_covered	=	$('#hid_non_covered').val();
		var	indiation_admit	=	$('#hid_indiation_admit').val();
		$.ajax({
			type: "POST",
			url: (projectName+"/front/payment/insertpayment/_t/"+date.getTime()).replace("//","/"),
			data: ({'payment_id' : payment_id,'userid' : userid,'preauth_id' :  preauth_id,'policy_no' :  policy_no,
				'payment_datetime' : payment_datetime,'payment_status' : payment_status,'customer_type' : customer_type,'policy_id' : policy_id,
				'payment_type' : payment_type,'acc_date' : acc_date,'admis_date' : admis_date,'deductible' : deductible,
				'charge' : charge,'discount' : discount,'co_payment' : co_payment,'pre_approved' : pre_approved,
				'non_covered' : non_covered,'indiation_admit' : indiation_admit}),
			dataType: 'json',
			success: function(msg){
				if(msg){
					window.open(app_url+'/print/'+policy_id,'_blank');
					$("#tbody_view_insert_payment").html(msg.htmls);
				}
				loff();
			}
		});
	});
}
function _renderPaTransection(){
	var preauth_id = $('#preauth_id').val();
	var tran_start_date = $('#tran_start_date').val();
	var tran_end_date = $('#tran_end_date').val();
	if(require(preauth_id) && (require(tran_start_date) && require(tran_end_date)))
		alert('กรุณากรอก Pre-auth ID หรือ วันที่เริ่มและสิ้นสุดรายการ');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/transection/loadtpl/tpl/_popup_pa_tran',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			$.ajax({
				type: "POST",
				url: (projectName+"/front/transection/getpreauthtranlist/_t/"+date.getTime()).replace("//","/"),
				data: ({'preauth_id':preauth_id,'tran_start_date':tran_start_date,'tran_end_date':tran_end_date}),
				dataType: 'json',
				success: function(msg){
					if(msg){
						$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderPaymentTransection(){
	var preauth_id = $('#preauth_id').val();
	var tran_start_date = $('#tran_start_date').val();
	var tran_end_date = $('#tran_end_date').val();
	if(require(preauth_id) && (require(tran_start_date) && require(tran_end_date)))
		alert('กรุณากรอก Pre-auth ID หรือ วันที่เริ่มและสิ้นสุดรายการ');
	else{
		lon();
		$('#loadAjaxContent').load(app_url+'/front/transection/loadtpl/tpl/_popup_payment_tran',function(){
			$('#link_preauth_fund').attr('href', '#view_preauth_list');
			$('#link_preauth_fund').addClass('fancybox-ajax-light');
			$('#link_preauth_fund').click();
			$('#closeFb').click(function(){
				closeLightBox('link_preauth_fund','fancybox-ajax-light');
			});
			$.ajax({
				type: "POST",
				url: (projectName+"/front/transection/getpaymenttranlist/_t/"+date.getTime()).replace("//","/"),
				data: ({'preauth_id':preauth_id,'tran_start_date':tran_start_date,'tran_end_date':tran_end_date}),
				dataType: 'json',
				success: function(msg){
					if(msg){
						$("#tbody_view_preauth_list").html(msg.htmls);
					}
					loff();
				}
			});
		});
	}
}
function _renderCalculate(){
	var preauth_id = $('#hid_preauth_id').val();
	var menu_seq = $('#hid_customer_type_desc').val();
	var cover_type = $('#cover_type').val();
	var acc_date;
	var charge = $('#txt_charge').val();
	var discount = $('#txt_discount').val();
	if($('#accList').val())
		acc_date = $('#accList').val();
	else
		acc_date = $('#txt_acc_date').val();
	
	if(require(acc_date))
		alert('กรุณากรอก Accident Date');
	else if(require(charge))
		alert('กรุณากรอก Charge');
	else if(require(discount))
		alert('กรุณากรอก Discount');
	else{
		lon();
		var preapv = eval($.ajax({
			type: "POST",
			url: (projectName+"/front/payment/calpreapprove/_t/"+date.getTime()).replace("//","/"),
			data: ({'preauth_id':preauth_id,'menu_seq':menu_seq,'cover_type':cover_type,'acc_date':acc_date,'charge':charge,'discount':discount}),
			async: false
		}).responseText);
		$('#txt_deductible').val(preapv[0]['deduct']);
		$('#txt_co_payment').val(preapv[0]['copay']);
		$('#txt_pre_approved').val(preapv[0]['preapv']);
		$('#txt_non_covered').val(preapv[0]['noncover']);
		loff();
	}
}